<template>
  <div class="goodsList">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card" style="min-width: 1280px;">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            class="input-with-select"
            clearable
            v-model="goodsList.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="primary" class="" @click="goAddPage"
            >添加商品</el-button
          >
        </el-col>
      </el-row>

      <el-table border :data="goodsList.data" stripe>
        <el-table-column type="index" label="#" width="46"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          width="91"
          prop="goods_price"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          width="70"
          prop="goods_weight"
        ></el-table-column>
        <el-table-column label="创建时间" width="140" prop="add_time">
          <template #default="{row}">
            {{ row.add_time | dateformat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{row}">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="putGoods(row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoods(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodsList.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="goodsList.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="goodsList.total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Goods',
  data() {
    return {
      goodsList: {
        data: [],
        query: '',
        pagenum: 1,
        pagesize: 10,
        total: 0,
      },
    };
  },
  methods: {
    // 修改商品
    putGoods(row) {
      // console.log(row);
    },
    // 删除商品
    delGoods(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return this.__apis.goods.deleteGoods(row.goods_id);
        })
        .then((res) => {
          if (res.data.meta.status === 200) {
            this.$message.success('删除成功');
            this.getGoodsList();
          }
        })
        .catch(() => {});
    },
    // 跳转到商品添加页面
    goAddPage() {
      this.$router.push('/goods/add');
    },
    // 每页数量改变
    handleSizeChange(size) {
      this.goodsList.pagesize = size;
      this.getGoodsList();
    },
    // 页码改变
    handleCurrentChange(current) {
      this.goodsList.pagenum = current;
      this.getGoodsList();
    },
    // 获取商品列表
    getGoodsList() {
      this.__apis.goods
        .getGoods({
          query: this.goodsList.query,
          pagenum: this.goodsList.pagenum,
          pagesize: this.goodsList.pagesize,
        })
        .then((res) => {
          // console.log(res);
          if (res.data.meta.status === 200) {
            this.goodsList.total = res.data.data.total;
            this.goodsList.data = res.data.data.goods;
          }
        });
    },
  },
  created() {
    this.getGoodsList();
  },
  filters: {
    dateformat(data) {
      const time = new Date(data);
      const year = time.getFullYear();
      let month = time.getMonth() + 1;
      let day = time.getDate();
      let hour = time.getHours();
      let minute = time.getMinutes();
      let second = time.getSeconds();
      month = month > 10 ? month : '0' + month;
      day = day > 10 ? day : '0' + day;
      hour = hour > 10 ? hour : '0' + hour;
      minute = minute > 10 ? minute : '0' + minute;
      second = second > 10 ? second : '0' + second;
      return `${year}-${month}-${day}  ${hour}:${minute}:${second}`;
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
  .el-col.el-col-8 {
    padding-right: 0;
  }
}
.el-table {
  margin: 15px 0;
  font-size: 12px;
}
.el-breadcrumb {
  font-size: 12px;
}
</style>
